//
//  UIViewController+JJDefBlankView.m
//  MicroMall
//
//  Created by chenzm on 2020/5/4.
//  Copyright © 2020 XIN. All rights reserved.
//

#import "UIViewController+JJDefBlankView.h"
#import <objc/runtime.h>
@implementation UIViewController (JJDefBlankView)


static char defBlankViewKey;
-(JJDefBlankView *)defBlankView{
 
    return objc_getAssociatedObject(self, &defBlankViewKey);
}
- (void)setDefBlankView:(JJDefBlankView *)defBlankView{
    objc_setAssociatedObject(self, &defBlankViewKey, defBlankView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(void)addBaseBlankView:(UIView *)view{
    self.defBlankView = [JJDefBlankView new];
    self.defBlankView.backgroundColor = kBgColor;
    [self.defBlankView setLabText:@"" btnText:@"" imageText:@""];
    self.defBlankView.hidden = YES;
    [view addSubview:self.defBlankView];
    [self.defBlankView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(view);
        make.size.mas_equalTo(view.size);
    }];
}

-(void)jj_blankViewShowIn:(UIView *)view {
    if (!self.defBlankView) {
        [self addBaseBlankView:view];
    }
}

-(void)jj_isHiddenBlankView:(BOOL)hidden{
    if (!self.defBlankView) {
        [self addBaseBlankView:self.view];
    }
    self.defBlankView.hidden=hidden;
    [self.defBlankView.superview bringSubviewToFront:self.defBlankView];
}

-(void)jj_setBlankViewTitle:(NSArray<NSString *> *)title{
    if (!self.defBlankView) {
        [self addBaseBlankView:self.view];
    }
    if (title.count==1) {
        [self.defBlankView setLabText:[title firstObject] btnText:@"" imageText:@""];
    }
 
    if (title.count==2) {
        [self.defBlankView setLabText:[title firstObject] btnText:@"" imageText:[title objectAtIndex:1]];
    }
    if (title.count>2) {
        [self.defBlankView setLabText:[title firstObject] btnText:[title objectAtIndex:1] imageText:[title objectAtIndex:2]];
    }
}




static char activityKey;
- (UIActivityIndicatorView *)activity{
    return objc_getAssociatedObject(self, &activityKey);
}
- (void)setActivity:(UIActivityIndicatorView *)activity{
    objc_setAssociatedObject(self, &activityKey, activity, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
}


-(void)jj_startActivityInView:(UIView *)view{
    if (!self.activity) {
        [self addActivityIn:view];
    }
    [view?view:self.view bringSubviewToFront:self.activity];
    [self.activity startAnimating];
}

-(void)jj_stopActivityInView:(UIView *)view{
    if (!self.activity) {
        [self addActivityIn:view];
    }
    [self.activity stopAnimating];
}


-(void)addActivityIn:(UIView *)view{
    self.activity= [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [self.activity setHidesWhenStopped:YES]; //当旋转结束时隐藏
    [view?view:self.view addSubview:self.activity];
    self.activity.center = view?view.center:self.view.center;
}

@end
